<?php
namespace hangpu8\hpcms;

use think\facade\Route;
use think\Service;

class MyService extends Service
{
    public function boot()
    {
        header('Content-Type: text/html;charset=utf-8');
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
        // 响应类型
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        // 响应头设置
        header("Access-Control-Allow-Headers: Content-Type,Authorization,CloudId,Eid");
        if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS')
        {
            exit;
        }
        $rootPath = str_replace("\\","/",dirname(dirname(dirname(dirname(__DIR__)))));
        $rootEnv = "{$rootPath}/.env";
        $routerCls = new \hangpu8\hpcms\Utils\Router;
        if (file_exists($rootEnv))
        {
            $this->registerRoutes(function ()use($routerCls) {
                // 动态注册路由
                $routes = $routerCls->getRouterAll();
                foreach ($routes as $item)
                {
                    $method = strtolower($item['method']);
                    Route::$method("/api{$item['router']}", $item['namespace']);
                }
                // 安装完成
                Route::rule("/install/step5", "\hangpu8\hpcms\Install@step5");
            });
        }else{
            // 设置安装程序所需要路由
            $this->registerRoutes(function () {
                // 欢迎页面
                Route::rule("/install/step1", "\hangpu8\hpcms\Install@step1");
                // 检测权限及环境
                Route::rule("/install/step2", "\hangpu8\hpcms\Install@step2");
                // 安装数据库
                Route::rule("/install/step3", "\hangpu8\hpcms\Install@step3");
                // 正在安装中
                Route::rule("/install/step4", "\hangpu8\hpcms\Install@step4");
            });
            // 进入安装环节
            $installCls = new \hangpu8\hpcms\Install;
            $installCls->run($this);
        }
    }
}